class SoldController < ApplicationController
  def new
  end

  def create
    @sold = Sold.createNew params['sold']['ref_id'],params['sold']['stock_id']
    @stock = Stock.find(@sold.stock_id)
    @sold.commission_percentage = @stock.commission
    @sold.tax_percentage = @stock.tax_percentage

    if @sold.save
       render :json => {:sold =>@sold,
                        :stock => @stock}
    else
       render :json => { }, :status => 500
    end
  end

  def update
    @sold = Sold.find(params[:sold][:id])
    @stock = Stock.find(@sold.stock_id)
    available_stock_quantity = @stock.quantity + @sold.quantity - params[:sold][:quantity].to_i
    @stock.quantity =   available_stock_quantity
    if @stock.save && @sold.update_attributes(params[:sold]) 
       render :json => {:sold =>@sold,
                        :stock => @stock}
    else
       render :json => @sold.to_json,:status => 500
    end

  end
end
